app
    .controller('treeCtrl', ['$scope', '$modal', '$http', '$stateParams', '$rootScope', '$filter', '$location', 'lodash', function ($scope, $modal, $http, $stateParams, $rootScope, $filter, $location, lodash) {
        $scope.info = {};
        $scope.info.treeTypes = [{"key": "维护产品线", "crtname": "维护产品线", "value": 0}, {
            "key": "维护流程",
            "crtname": "维护流程",
            "value": 1
        }]
        $scope.info.treeType = $scope.info.treeTypes[0];
        $scope.my_tree = {};
        $scope.info.selectInfo = null;
        $scope.url = {};


        $rootScope.initBreadcrumb($scope.breadcrumb);
        $scope.breadcrumb.push('基础配置','维护树配置');


        $scope.getTree = function () {
            if ($scope.info.treeType.value == 0) {
                $scope.url = {
                    "getTree": "/productLine/getProductLineTree",
                    "getTreeId": "/productLine/deleteProductLine?productLineId=",
                    "saveTree": "/productLine/saveProductLine",
                    "deleteTree": "/productLine/deleteProductLine?productLineId="
                }
            } else {
                $scope.url = {
                    "getTree": "/flowFrame/getFlowFrameTree",
                    "getTreeId": "/flowFrame/getFlowFrameTree?flowFrameId=",
                    "saveTree": "/flowFrame/saveFlowFrame",
                    "deleteTree": "/flowFrame/deleteFlowFrame?flowFrameId="
                }
            }
            $http.get(matrix_product_url + $scope.url.getTree).success(function (data) {
                console.log(data);
                var itemArray = [];
                itemArray.push(data.data);
                $scope.info.tree = itemArray;
            })
        }

        $scope.treeTypeClick = function () {
            $scope.getTree();
            $scope.info.selectInfo = null;
        }
        $scope.treeTypeClick();

        $scope.select = function (info) {
            $scope.info.selectInfo = info;
            console.log($scope.info.selectInfo)

        }

        $scope.delete = function (productLine) {
            var confirmPrompt = {
                titleData: "提示",
                bodyData: "是否删除" + productLine.name + "？"
            };
            $rootScope.confirmWindow(confirmPrompt, function (confirm) {
                $http.get(matrix_product_url + $scope.url.deleteTree + productLine.id).success(function (data) {
                    if (data.code == 0) {
                        lodash.remove($scope.my_tree.get_parent_branch(productLine).children, {id: productLine.id})
                        $scope.toasterSuccessInfo("删除成功");
                        $scope.info.selectInfo = null;
                    } else {
                        $rootScope.toasterErrorInfo(data.desc);
                    }
                })
            })
        }

        $scope.edit = function (type, info) {
            var modalInstance = $modal.open({
                animation: true,
                templateUrl: 'editProductLine.html',
                controller: 'editProductLineCtrl',
                keyboard: false,
                backdrop: 'static',
                resolve: {
                    type: function () {
                        return type;
                    },
                    selectInfo: function () {
                        return info;
                    }
                }
            });

            modalInstance.result.then(function (productLineDetails) {
                editProductLine(productLineDetails);
            }, function (reason) {
                console.log("关闭modal");
            });
        }

        function editProductLine(productLineDetails) {
            var currentType = productLineDetails.lineStatus;
            delete productLineDetails.lineStatus;
            if (currentType == "add") {
                productLineDetails.level = parseInt(productLineDetails.level) + 1
            }
            var saveUrl = matrix_product_url + $scope.url.saveTree;
            $http({
                method: "post",
                url: saveUrl,
                data: JSON.stringify(productLineDetails),
                headers: {"Content-Type": "application/json"}
            }).success(function (result) {
                if (result.code == "0") {
                    if (currentType == "add") {
                        $http.get(matrix_product_url + "/productLine/getProductLineTree?productLineId=" + result.data).success(function (dataInfo) {
                            $scope.info.selectInfo.children.push(dataInfo.data)
                        })
                    }
                    $scope.toasterSuccessInfo(currentType == "add" ? "创建成功！" : "修改成功！");
                } else {
                    $rootScope.toasterErrorInfo(result.desc);
                }
            });

        };

    }])
app.controller("editProductLineCtrl", ["$scope", "$rootScope", "$modalInstance", "$http", "$filter", "lodash", "type", "selectInfo", function ($scope, $rootScope, $modalInstance, $http, $filter, lodash, type, selectInfo) {
    $scope.info = {
        type: type,
        productLine: {},
    };
    if ($scope.info.type == 'add') {
        $scope.info.productLine = {"parentId": selectInfo.id, "level": selectInfo.level};
    } else {
        $scope.info.productLine = selectInfo;
    }
    $scope.info.productLine.lineStatus = type;
    $scope.save = function () {
        $modalInstance.close($scope.info.productLine);
    }

    $scope.cancel = function () {
        $modalInstance.dismiss("cancel");
    }
}])